
/**
 * @class Ext.ux.Filter.Authors
 * @extends Ext.ux.Filter
 * A configurable filter for filtering tags on a given view.
 * Requests tag data for the current object from the server
 */
Ext.ux.Filter.Authors = Ext.extend(Ext.ux.Filter, {

			constructor		: function() {
				Ext.ux.Filter.Authors.superclass.constructor.apply(this,
						arguments
				);

				this.store = new Ext.data.JsonStore({
							root	: 'authors',
							url		: '/authors/for',
							fields	: ['author_membership_id',
									'author_nickname']
						});

				Ext.applyIf(this, {
							title	: Ext.ux.Translator.translate('authors'),
							name	: 'authors',
							iconCls	: 'author'
						});
			},

			/**
			* Overrides the default eventReceived.  If the first argument is a model instance the
			* store's url is updated to match the URL for this particular instance, and the store
			* is reloaded.  If not a model instance, this just refreshes.
			* @param {Ext.data.Store} store The store full of records
			*/
			eventReceived	: function(store) {
				// TODO: this is not a good way of doing this test, but instanceof won't work until
				// MVC 0.6, at which point we can use instance instanceof Ext.data.Record
				if (store && store.data && store.data.items
						&& store.data.items[0]
						&& typeof store.data.items[0].newRecord != 'undefined') {
					var instance = store.data.items[0];

					this.store.proxy.conn.url = this.urlFor(instance);

					this.store.reload({
								scope		: this,
								callback	: this.refresh
							});

					// keep a reference to the currently loaded instance so as not to refresh too often
					this.currentModelInstance = instance;
				} else if (typeof store.newRecord != 'undefined') {
					this.store.proxy.conn.url = this.urlFor(store);

					this.store.reload({
								scope		: this,
								callback	: this.refresh
							});

					// keep a reference to the currently loaded instance so as not to refresh too often
					this.currentModelInstance = store;
				} else {
					this.refresh(store);
				};
			},

			refresh			: function(store) {
				this.options = [];
				var authors = [];

				// if (store && store.totalLength == 0) {
				// return this.fireEvent('refreshed', this);
				// };

				if (!this.store)
					return;

				// Don't allow duplicates
				this.store.each(function(item) {
							if (!item.get('author_membership_id'))
								return;

							var obj = {
								id		: item.get('author_membership_id'),
								text	: item.get('author_nickname')
							};

							var inIt = false;

							for (var i = 0; i < authors.length; i++) {
								if (authors[i].id == obj.id)
									inIt = true;
							};

							if (!inIt)
								authors.push(obj);
						}, this);

				Ext.each(authors, function(author) {
							this.options.push({
										id		: author.id,
										text	: author.text,
										iconCls	: this.iconCls
									});
						}, this);

				this.options.sort(this.sortAlphabetically);

				this.fireEvent('refreshed', this);
			},

			/**
			 * Builds the url to retrieve tags from for a given model instance
			 * @param {ExtMVC.Model.Base} instance The model instance
			 * @return {String} The url for this instance
			 */
			urlFor			: function(instance) {
				var className = instance.className;

				var id = (className == "Image")
						? instance.get('album_id')
						: Kohive.OS.getCurrentHiveId();
				var url = (this.name == "recipients")
						? "recipients"
						: "authors";

				if (className == "File") {
					className = "FileCabinet";
				};

				return String.format("/{0}/for/{1}/{2}", url, className, id);
			}
		}
);

Ext.ux.FilterManager.reg('authors', Ext.ux.Filter.Authors);
